package yunwo.cn.yzstation.view.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import com.zhy.http.okhttp.request.RequestCall;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import yunwo.cn.yzstation.R;
import yunwo.cn.yzstation.adapter.EndOrganAdapter;
import yunwo.cn.yzstation.adapter.ScreenAllAdapter;
import yunwo.cn.yzstation.adapter.TansAdapter;
import yunwo.cn.yzstation.api.HttpManager;
import yunwo.cn.yzstation.api.okapi.GLCallBack;
import yunwo.cn.yzstation.base.MyBaseActivity;
import yunwo.cn.yzstation.base.Yzapp;
import yunwo.cn.yzstation.bean.DictionariesBean;
import yunwo.cn.yzstation.bean.PackBean;
import yunwo.cn.yzstation.bean.PackProductBean;
import yunwo.cn.yzstation.config.GoodsSP;
import yunwo.cn.yzstation.config.MyConstants;
import yunwo.cn.yzstation.other.PickDialog;
import yunwo.cn.yzstation.other.ShowAnimationDialogUtil;
import yunwo.cn.yzstation.utils.StringUtil;

/**
 * Created by lao on 2017/9/16.
 */
public class ProductActivity extends MyBaseActivity implements View.OnClickListener{

    private TextView mGoodsTV;
    private TextView mPackingTV;
    private PickDialog mHalfScreenDialog;
    private SharedPreferences mDictionariesBean_sp;
    private ArrayList<String> mPinmingList;
    private HashMap<String, String> mPinmingmap;
    private ArrayList<String> mBaozhuanglist;
    private HashMap<String, String> mBaozhuangmap;
    private TextView mConfirmTV;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initNativeData();
        getData();
        initView();

    }

    private void initNativeData() {

        mDictionariesBean_sp = getSharedPreferences("SP_DICTION", Context.MODE_PRIVATE);
        String zidian = mDictionariesBean_sp.getString("zidian", "");
        if (!StringUtil.isEmpty(zidian)) {
            Gson gson = new Gson();
            DictionariesBean dictionariesBean = gson.fromJson(zidian, DictionariesBean.class);
            mPinmingList = new ArrayList<>();
            mPinmingmap = new HashMap<>();
            mBaozhuanglist = new ArrayList<>();
            mBaozhuangmap = new HashMap<>();

            for (int i = 0; i < dictionariesBean.getData().size(); i++) {
                if (dictionariesBean.getData().get(i).getDictCode().equals("PRODUCT_NAME_TYPE")) {
                    mPinmingList.add(dictionariesBean.getData().get(i).getDictName());
                    mPinmingmap.put(dictionariesBean.getData().get(i).getDictName(), dictionariesBean.getData().get(i).getDictValue());
                }
            }
            for (int i = 0; i < dictionariesBean.getData().size(); i++) {
                if (dictionariesBean.getData().get(i).getDictCode().equals("PACK_NAME_TYPE")) {
                    mBaozhuanglist.add(dictionariesBean.getData().get(i).getDictName());
                    mBaozhuangmap.put(dictionariesBean.getData().get(i).getDictName(), dictionariesBean.getData().get(i).getDictValue());
                }
            }

        }
    }

    private void getData() {

        Map<String, Object> map = new HashMap<>();
        HttpManager.hpptPostString3(MyConstants.getPackProduct,map).execute(new GLCallBack() {
            @Override
            public void onResponseT(String result, int code, String msg, int id) {
                if (code == 200) {

                    if (!result.equals("\"\"")) {
                        PackProductBean.DataBean dataBean = new Gson().fromJson(result, PackProductBean.DataBean.class);
                        if (mGoodsTV != null && mPackingTV != null) {
                            mGoodsTV.setText(dataBean.getProductName());
                            mPackingTV.setText(dataBean.getPackName());
                        }
                        Yzapp.getInstance().getinfoSPEditor().putString("packName", dataBean.getPackName());
                        Yzapp.getInstance().getinfoSPEditor().putString("productName", dataBean.getProductName());
                        Yzapp.getInstance().getinfoSPEditor().commit();
                    }
                }
            }

            @Override
            public void onError(Call call, Exception e, int id) {

            }
        });

    }

    private void initView() {
        mGoodsTV = (TextView) findViewById(R.id.goodsTV);
        mConfirmTV = (TextView) findViewById(R.id.confirmTV);
        mPackingTV = (TextView) findViewById(R.id.packingTV);
        mGoodsTV.setText(Yzapp.getInstance().getproductName());
        mPackingTV.setText(Yzapp.getInstance().getPackingname());
        mGoodsTV.setOnClickListener(this);
        mPackingTV.setOnClickListener(this);
        mConfirmTV.setOnClickListener(this);
    }

    @Override
    protected String settiltile() {
        return "设置品名/包装";
    }

    @Override
    protected int getLayoutRsid() {
        return R.layout.activity_set_packing;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.goodsTV:
                screening("3");
                break;
            case R.id.packingTV:
                screening("4");
                break;
            case R.id.confirmTV:
                confirm();
                break;
        }
    }

    private void confirm() {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> map2 = new HashMap<>();
        map.put("productId", mPinmingmap.get(mGoodsTV.getText().toString().trim()));
        map.put("productName", mGoodsTV.getText().toString().trim());
        map.put("packId", mBaozhuangmap.get(mPackingTV.getText().toString().trim()));
        map.put("packName", mPackingTV.getText().toString().trim());
        map2.put("configureValue", map);
HttpManager.hpptPostString3(MyConstants.setPackProduct,map2).execute(new GLCallBack() {
    @Override
    public void onResponseT(String result, int code, String msg, int id) {
        if (code == 200) {
            Toast.makeText(ProductActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
            Yzapp.getInstance().getinfoSPEditor().putString("packName", mPackingTV.getText().toString().trim());
            Yzapp.getInstance().getinfoSPEditor().putString("productName",  mGoodsTV.getText().toString().trim());
            Yzapp.getInstance().getinfoSPEditor().commit();
        }else {
            Toast.makeText(ProductActivity.this, msg, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onError(Call call, Exception e, int id) {

    }
});
/*        Map<String, Object> map = new HashMap<>();
        Map<String, Object> map2 = new HashMap<>();
        map.put("packId",mBaozhuangmap.get(mPackingTV.getText().toString().trim()));
        map.put("packName", mPackingTV.getText().toString().trim());
        map2.put("configureValue", map);
        HttpManager.hpptPostString3(MyConstants.setPack,map2).execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Log.d("gggggggggg", e.toString());
            }

            @Override
            public void onResponse(String response, int id) {
                Log.d("gggggggggg", response);
            }
        });
        Map<String, Object> map3 = new HashMap<>();
        Map<String, Object> map4 = new HashMap<>();
        map3.put("productId", mPinmingmap.get(mGoodsTV.getText().toString().trim()));
        map3.put("productName", mGoodsTV.getText().toString().trim());
        map4.put("configureValue", map3);
        HttpManager.hpptPostString3(MyConstants.setProduct,map4).execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Log.d("gggggggggg", e.toString());
            }

            @Override
            public void onResponse(String response, int id) {
                Log.d("gggggggggg", response);
            }
        });*/

    }


    public void screening(final String type) {
        mHalfScreenDialog = ShowAnimationDialogUtil.showDialog(ProductActivity.this,
                R.layout.screen_view, Gravity.RIGHT, false, true);
        initHalfScreenView(mHalfScreenDialog, type);
    }


    private void initHalfScreenView(PickDialog dateDialog, final String type) {
        View view = dateDialog.getView();
        TextView titleTV = (TextView) view.findViewById(R.id.titleTV);
        ListView halfScreenLV = (ListView) view.findViewById(R.id.halfScreenLV);
        if ("3".equals(type)) {
            titleTV.setText("品名");
            ScreenAllAdapter screenAllAdapter = new ScreenAllAdapter(mPinmingList, ProductActivity.this);
            halfScreenLV.setAdapter(screenAllAdapter);
            halfScreenLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    setDialogDismiss(mHalfScreenDialog);
                    mGoodsTV.setText(mPinmingList.get(position));

                }
            });
        } else if ("4".equals(type)) {
            titleTV.setText("包装");
            ScreenAllAdapter screenAllAdapter = new ScreenAllAdapter(mBaozhuanglist, ProductActivity.this);
            halfScreenLV.setAdapter(screenAllAdapter);
            halfScreenLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    setDialogDismiss(mHalfScreenDialog);
                    mPackingTV.setText(mBaozhuanglist.get(position));

                }
            });
        }

    }
    private void setDialogDismiss(PickDialog dateDialog) {
        if (dateDialog != null && dateDialog.isShowing()) {
            dateDialog.dismiss();
        }
    }
}
